package com.example.boot3.controller;


import com.alibaba.excel.EasyExcel;
import com.example.boot3.model.Card;
import com.example.boot3.model.Recharge;
import com.example.boot3.model.Refuel;
import com.example.boot3.model.Vehicle;
import com.example.boot3.service.CardService;
import com.example.boot3.util.JsonData;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@CrossOrigin(origins =  "*")
@RestController
//@RequestMapping("/api")
public class CardController {
    @Autowired
    CardService cardService;
    @GetMapping("card_info")
    public JsonData getFuelCardsList(){
        return JsonData.buildSuccess(cardService.getFuelCardsList());
    }

    @GetMapping("recharge_info")
    public  JsonData getRechargeList(){
        return JsonData.buildSuccess(cardService.getRechargeList());
    }

    @GetMapping("refuel_info")
    public JsonData getRefuelList(){
        return JsonData.buildSuccess(cardService.getRefuelList());
    }

    @PostMapping("add_card")
    public JsonData addCard(@RequestBody Card card){
        cardService.addCard(card);
        return JsonData.buildSuccess(null);
    }
    @PostMapping("recharge_card")
    public JsonData rechargeCard(@RequestBody Recharge recharge){
        cardService.rechargeCard(recharge);
        return JsonData.buildSuccess(null);
    }
    @PostMapping("refuel_card")
    public JsonData applyRefuel(@RequestBody Refuel refuel){
        cardService.applyRefuel(refuel);
        return JsonData.buildSuccess(null);
    }



    @GetMapping("card/export")
    public void cardExport(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=cards.xlsx");
        List<Card> data = cardService.getFuelCardsList();
        EasyExcel.write(response.getOutputStream(), Card.class).sheet("油卡信息").doWrite(data);
    }

    @GetMapping("recharge/export")
    public void rechargeExport(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=recharges.xlsx");
        List<Recharge> data = cardService.getRechargeList();
        EasyExcel.write(response.getOutputStream(), Recharge.class).sheet("充值信息").doWrite(data);
    }

    @GetMapping("refuel/export")
    public void refuelExport(HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment; filename=refuels.xlsx");
        List<Refuel> data = cardService.getRefuelList();
        EasyExcel.write(response.getOutputStream(), Recharge.class).sheet("加油信息").doWrite(data);
    }





}
